Copy Readable LinkするUserScript
実装がやや雑で絵文字もエンコードされてしまうがまあいいや
@shokai: @egpl0 説明が難しいけど最近iOS Slackの挙動がおかしいので一時的に消しました なぜiOS Slackの挙動がおかしいことで機能が消えるのかよくわからないが、色々あるのだろう
code:script.js
// @ts-check
scrapbox.PageMenu.addMenu({
title: 'copy readable link',
onClick: main,
});
async function main() {
const project = projectName();
const title = pageTitle();
const readableTitle = encodeTitleForReadableUrl(title);
const readableUrl = https://scrapbox.io/${project}/${readableTitle};
await navigator.clipboard.writeText(readableUrl);
}
/**
* Encodes only the unsafe characters in the title to make it URL-safe while preserving readability.
* @param {string} title
* @returns {string}
*/
function encodeTitleForReadableUrl(title) {
return title
.split('') // Process each character individually
.map(char => {
// Allow spaces to be replaced with underscores
if (char === ' ') return '_';
// Allow alphanumeric characters and safe symbols
if (/\w\-_.~/.test(char)) return char; // Allow Kanji (CJK Unified Ideographs)
// Allow Hiragana
// Allow Katakana
// Allow Full-width alphanumeric and symbols
// Allow Emoji and other symbols
// Encode all other characters
return encodeURIComponent(char);
})
.join('');
}
/**
* @returns {string} The encoded project name.
*/
function projectName() {
return scrapbox.Project.name;
}
/**
* @returns {string} The encoded page title.
*/
function pageTitle() {
return scrapbox.Page.title;
}